//
//  LoginViewController.m
//  Teacher
//
//  Created by cdm on 15/7/6.
//  Copyright (c) 2015年 xueyinyue. All rights reserved.
//

#import "FirstPageViewController.h"
#import "NSString+WPAttributedMarkup.h"
#import "UserBSLoginWithVerification.h"
#import "UserBSLoadCurrentUser.h"
#import "User.h"
#import "ApplicationContext.h"

#import <CoreText/CoreText.h>

@interface FirstPageViewController ()

@end

@implementation FirstPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
 //  [self autoLogin];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)autoLogin
{
    //如果存有用户名，直接进入主界面
    UserBSLoadCurrentUser *userBSLoadCurrentUser = [[UserBSLoadCurrentUser alloc] init];
    [self runService:userBSLoadCurrentUser
         andComplete:^(EACService *service) {
             
         }
          andSuccess:^(EACService *service, id o) {
              UserBSLoadCurrentUserResult *userBSLoadCurrentUserResult = o;
              if (userBSLoadCurrentUserResult.data)
              {
                  NSLog(@"已存在用户，直接进入主界面");
                  //设置当前用户
                  [ApplicationContext instance].currentUser = userBSLoadCurrentUserResult.data;
                  
                  //跳转至主界面
              }
              else
              {
                  NSLog(@"不存在用户，请登录");
              }
          }];
}

#pragma mark - LoginViewControllerDelegate

- (void)loginViewControllerDidCancel:(LoginViewController *)controller {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - RegisterViewControllerDelegate

- (void)registerViewControllerDidCancel:(RegisterViewController *)controller {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField{
   
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


#pragma mark - Event

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

/**
 *  登录按钮响应事件
 */
- (IBAction)onLoginButtonClick:(id)sender {
    
    LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    loginViewController.delegate = self;
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
    navigationController.navigationBar.translucent = NO;
    [self presentViewController:navigationController animated:YES completion:nil];
}

/**
 *  注册按钮响应事件
 */
- (IBAction)onRegisterButtonClick:(id)sender {
    
   RegisterViewController *registerViewController = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
    registerViewController.delegate = self;
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:registerViewController];
    navigationController.navigationBar.translucent = NO;
    [self presentViewController:navigationController animated:YES completion:nil];
    
}

@end
